import { _decorator, Component, instantiate, Prefab, ProgressBar, resources, tween, TweenEasing, UIOpacity, v3 } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
import { Ab_ClientAPI_bA } from '../../api/client.api';
import { Ab_AudioManager_bA } from '../../manager/audio-manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameManager_bA from '../../manager/game.manager';
const { ccclass, property } = _decorator;

@ccclass('Ab_loadGame')
export class Ab_loadGame extends Component {

	@property(ProgressBar)
	private bar: ProgressBar = null;

	private prmz_init: Promise<void>;

    start() {
		this.Ab_GameInit_bA();
		this.Ab_loadGame_bA();
		this.Ab_adaptUI_bA();
    }

	private Ab_adaptUI_bA(){
		abi.Ab_cc_bA.Ab_adaptBackgroundNode_bA(this.node.getChildByName('background'));
	}

	private Ab_GameInit_bA(){
		this.prmz_init = Ab_GameManager_bA.Ab_init_bA("US");
	}

	private Ab_progress_data_bA: Array<{total: number, finished: number, done: boolean}> = [];
	private Ab_updateProgress_bA(index: number, total: number, finished: number){
		if(!this.Ab_progress_data_bA[index]) this.Ab_progress_data_bA[index] = {total, finished, done: false};
		else Object.assign(this.Ab_progress_data_bA[index], {total, finished});

		let total_num = this.Ab_progress_data_bA.reduce((acc, cur)=>acc+cur.total, 0);
		let finished_num = this.Ab_progress_data_bA.reduce((acc, cur)=>acc+cur.finished, 0);
		let progress = Math.min(1, finished_num / total_num);
		this.bar.progress = progress;
	}

	private Ab_loadGame_bA(){
		['home-bg', 'game', 'home-ui', 'transition', 'pop-up', 'float', 'guide'].forEach((name, index)=>resources.load(
			'prefab-game/page/'+name, Prefab,
			(finished, total)=>{
				this.Ab_updateProgress_bA(index, total, finished);
			},(err, prefab)=>{
				let node = instantiate(prefab);
				node.active = false;
				node.setParent(this.node.parent);
				this.Ab_progress_data_bA[index].done = true;
				if(this.Ab_progress_data_bA.every(el=>el.done)) this.Ab_loadDone_bA();
			})
		);
	}

	private Ab_loadDone_bA(){
		['Camera', 'audio', 'home-bg', 'game', 'home-ui', 'transition', 'pop-up', 'float', 'guide', 'loading']
		.forEach((name, index)=>this.node.parent.getChildByName(name).setSiblingIndex(index));

		this.prmz_init.then(()=>{
			this.node.parent.children.forEach(node=>node.name=='Camera' ? void 0 : node.active = true);
			this.Ab_otherPreprocess_bA().then(()=>this.Ab_enterGame_bA());
		});
	}

	private Ab_otherPreprocess_bA(){
		Ab_ClientAPI_bA.Ab_CloseLoading_bA();
		let index = this.Ab_progress_data_bA.length;
		let np_pltp = new abi.Ab_NudityPromise_bA<'end'>();
		let reg_pltp = Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_PreLoadTilesProgress_bA', ()=>{
			let [t, f] = Ab_GameDC_bA.Ab_Trigger_bA.Ab_PreLoadTilesProgress_bA.split(',').map(Number);
			this.Ab_updateProgress_bA(index, t, f);
			if(t===f){
				Ab_DataTransfer_bA.Ab_cancel_bA(reg_pltp);
				np_pltp.Ab_resolve_bA('end');
			}
		}, false);
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_PreLoadTiles_bA = true;

		let np_guide = new abi.Ab_NudityPromise_bA<'end'>();
		if(Ab_GameDC_bA.Ab_Status_bA.Ab_guideInited_bA) np_guide.Ab_resolve_bA('end');
		else{
			let reg = Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Status_bA, 'Ab_guideInited_bA', (_, inited)=>{
				Ab_DataTransfer_bA.Ab_cancel_bA(reg);
				np_guide.Ab_resolve_bA('end');
			}, false);
		}

		return Promise.all([np_pltp.Ab_promise_bA, np_guide.Ab_promise_bA]);
	}


	private Ab_enterGame_bA(){
		let [bg, logo, bar] = ['background', 'logo', 'progress-bar'].map(name=>this.node.getChildByName(name));

		let [va_logo, va_bar] = [logo, bar].map(n=>new abi.Ab_cc_bA.Ab_VecAssist_bA(n));
		va_logo.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_top_bA').x = logo.position.x;
		va_bar.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_bottom_bA').x = bar.position.x;

		let duration = 0.8;
		const easing: TweenEasing = 'cubicOut';
		tween(logo).to(duration, {position: va_logo.Ab_vec_bA}, {easing}).start();
		tween(bar).to(duration, {position: va_bar.Ab_vec_bA}, {easing}).start();

		//let [bg, bar] = ['background', 'progress-bar'].map(name=>this.node.getChildByName(name));

		//let va_bar = new abi.Ab_cc_bA.Ab_VecAssist_bA(bar);
		//va_bar.Ab_getPosition_bA('Ab_center_bA', 'Ab_over_bottom_bA').x = bar.position.x;

		//let duration = 0.8;
		//const easing: TweenEasing = 'cubicOut';
		//tween(bar).to(duration, {position: va_bar.Ab_vec_bA}, {easing}).start();

		let opt = bg.getComponent(UIOpacity);
		if(!opt) opt = bg.addComponent(UIOpacity);

		tween(opt).to(duration, {opacity: 0}, {easing}).start();
		tween(bg).to(duration, {scale: v3(1.5, 1.5, 1)}, {easing})
		.call(()=>{
			this.node.destroy();
			Ab_AudioManager_bA.Ab_playBGM_bA();
		}).start();

		Ab_GameDC_bA.Ab_Trigger_bA.Ab_LoadRound_bA = Ab_GameDC_bA.Ab_User_bA.Ab_round_bA;
		//Ab_GameDC_bA.Ab_Trigger_bA.Ab_LoadRound_bA = 4;
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIHome_bA = true;
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIGame_bA = true;
		Ab_AudioManager_bA.Ab_mute_bA(!Ab_GameDC_bA.Ab_User_bA.Ab_bgm_bA);
	}
}
